package tliasmanagement.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import tliasmanagement.anno.Log;
import tliasmanagement.pojo.Result;
import tliasmanagement.utils.AliyunOSSOperator;

import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    @Log
    @PostMapping("/upload")
    public Result upload (MultipartFile file) throws Exception {
        log.info("文件上传，参数：{}", file);
        if (!file.isEmpty()) {
            // 生成文件名
            String originalFileName = file.getOriginalFilename();
            String extName = originalFileName.substring(originalFileName.lastIndexOf("."));
            String uniqueFileName = UUID.randomUUID().toString() + extName;

            // 上传文件
            String url = aliyunOSSOperator.upload(file.getBytes(), uniqueFileName);
            return Result.success(url);
        }
        return Result.error("上传失败");
    }
}
